<?php

class IndexController extends CustomControllerAction
{
	
    public function init()
    {
        parent::init();
    }

    public function indexAction()
    {
        // action body
        $this->logger->debug("indexAction entered");

        // get info
        $request = $this->getRequest();
        
        $name 		= $request->getParam('name');
        $password 	= $request->getParam('password');
        $email 		= $request->getParam('email');
        $birthday 	= $request->getParam('birthday');
        
        // validate
        $name_validator 	= new Zend_Validate_Regex(array('pattern'=>'/^[a-zA-Z0-9\-_]{3,20}$/'));
        $password_validator = new Zend_Validate_StringLength(array('min'=>5, 'max'=>100));
        $email_validator 	= new EmailValidator();
        $birthday_validator = new Zend_Validate_Date(array('format'=>'dd MMMM yyyy', 'locale'=>'en'));
        
        $valid = true;
        if (!$name_validator->isValid($name)) {
        	$valid = false;
        	foreach ($name_validator->getMessages() as $msg)
        		$this->logger->err("Name $name is invalid: $msg");
        } else if (!$password_validator->isValid($password)) {
        	$valid = false;
        	foreach ($password_validator->getMessages() as $msg)
        		$this->logger->err("Password $password is invalid: $msg");
        } else if (!$email_validator->isValid($email)) {
        	$valid = false;
        	foreach ($email_validator->getMessages() as $msg)
        		$this->logger->err("Email $email is invalid: $msg");
        } else if (!$birthday_validator->isValid($birthday)) {
        	$valid = false;
        	foreach ($birthday_validator->getMessages() as $msg)
        		$this->logger->err("Birthday $birthday is invalid: $msg");
        }
        
        // store to the database
        if ($valid)
        {
        	$users = new UsersTable();
	        $users->AddUser($name, $password, $email, $birthday);
        }
        
        $this->logger->debug("indexAction finished");
    }
    
    public function testdbAction()
    {
    	$this->_helper->viewRenderer->setNoRender();
        $this->logger->debug('testdbAction started');
        
        $users_table = new UsersTable();
        $select = $users_table->select()->where('user_id = ?', 2);
        $user = $users_table->fetchRow($select);      

        $this->logger->debug('name=' . $user->name);
        $this->logger->debug('password=' . $user->password);
        
        $this->logger->debug('testdbAction finished');
    }

    public function myAction()
    {
       $this->_helper->viewRenderer->setNoRender();
       Zend_Registry::get('logger')->debug("myAction started");
       echo "myAction\n";
       $arr = array();
       $arr['var2'] = 'val2';
       $this->_forward("your", "index", "", $arr);
    }

    public function yourAction()
    {
       $this->_helper->viewRenderer->setNoRender();
       echo "yourAction (var1=". $this->getRequest()->getParam('var1') 
               .", var2=".  $this->getRequest()->getParam('var2') .")\n";
       
       $this->_helper->_flashMessenger->AddMessage("message to them");
       $arr = array(); $arr["var3"] = "blah blah";
       $this->_helper->_flashMessenger->AddMessage($arr);
       $this->_redirect("/error/their");
    }

}

